package test.core;

import java.util.concurrent.CountDownLatch;

import hyl.core.info.Msg;
import hyl.core.run.IDo1;

import hyl.core.struct.MsgManager;

public class testThreadEvent {
	static MsgManager<Integer,byte[]> _MM = new MsgManager<>("事件");
	public  static void main(String[] args) {
		 test开始模拟秒杀式消费();

		
		
		
	}
	private static final int BINGFA = 40;
	private static CountDownLatch cd1 = new CountDownLatch(BINGFA);


	public static void test发送() {
		//构造 
		for(int i=0;i<BINGFA;i++) {
			_MM.do写入消息(i%2, ("测试"+i).getBytes());
			cd1.countDown();
		}
	}
	
	public static void test注册订阅1() {
		IDo1<Msg<Integer,byte[]>> handle1=new IDo1<Msg<Integer,byte[]>>(){
			@Override
			public void run(Msg<Integer,byte[]> obj) {
				//System.err.println(new String (obj.bb事件));
				obj.remark=1;
			}			
		};
		IDo1<Msg<Integer,byte[]>> handle2=new IDo1<Msg<Integer,byte[]>>(){
			@Override
			public void run(Msg<Integer,byte[]> obj) {
				
				//System.err.print((int)obj.remark);
				obj.remark=2;
			}			
		};
		IDo1<Msg<Integer,byte[]>> handle3=new IDo1<Msg<Integer,byte[]>>(){
			@Override
			public void run(Msg<Integer,byte[]> obj) {
				
				obj.remark=3;
			
				System.err.println(new String (obj.getValue()));
				//System.err.println("结束");
			}			
		};
		_MM.do注册(1,handle1);
		_MM.do注册(1,handle2);
		_MM.do注册(1,handle3);

	}
	public static void test注册订阅2() {
		IDo1<Msg<Integer,byte[]>> handle1=new IDo1<Msg<Integer,byte[]>>(){
			@Override
			public void run(Msg<Integer,byte[]> obj) {
				System.err.println(new String (obj.getValue()));
				obj.remark=1;
			}			
		};
		IDo1<Msg<Integer,byte[]>> handle2=new IDo1<Msg<Integer,byte[]>>(){
			@Override
			public void run(Msg<Integer,byte[]> obj) {
				
				//System.err.println((int)obj.remark);
				obj.remark=2;
			}			
		};


		_MM.do注册(0,handle1);
		_MM.do注册(0,handle2);

		
	}
	public static void test开始模拟秒杀式消费() {
		try {
			//先注册
			test注册订阅2();
			test注册订阅1();
		
			new Thread(()-> test发送()).start();
			//后消费
			cd1.await();
		
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
